package com.iraeta.service.basic.impl;

import cn.hutool.core.date.DateUtil;
import com.iraeta.dao.main.basic.SrmmessageDao;
import com.iraeta.entity.main.Srmmessage;
import com.iraeta.entity.main.type.SrmMessageStatus;
import com.iraeta.service.basic.SrmmessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SrmmessageServiceImpl implements SrmmessageService {
    @Resource
    SrmmessageDao srmmessageDao;

    @Override
    public int add(Srmmessage record) {
        if(srmmessageDao.selectByOrderId(record.getOrderid()) == null){
            return srmmessageDao.insert(record);
        }
        return 0;
    }

    @Override
    public int update(Srmmessage record) {
        return srmmessageDao.updateByPrimaryKey(record);
    }

    @Override
    public int delete(Long id) {
        return srmmessageDao.deleteByPrimaryKey(id);
    }

    @Override
    public Srmmessage getById(Long id) {
        return srmmessageDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Srmmessage> ListCanProcessMsg() {
        return srmmessageDao.selectCanProcessMsg(SrmMessageStatus.saveToErp);
    }

    @Override
    public List<Srmmessage> listByCondition(String typeName, String orderData, String startDate, String endDate) {
        String endDataStr="";
        if(endDate != null){
            endDataStr = DateUtil.format(DateUtil.offsetDay(DateUtil.parseDate(endDate), 1), "yyyy-MM-dd");
        }
        return srmmessageDao.selectByCondition(typeName,orderData,startDate,endDataStr);
    }

    @Override
    public List<Srmmessage> listErrorMsg(String typeName, String orderData, String startDate, String endDate) {
        String endDataStr="";
        if(endDate != null){
            endDataStr = DateUtil.format(DateUtil.offsetDay(DateUtil.parseDate(endDate), 1), "yyyy-MM-dd");
        }
        return srmmessageDao.selectErrorMsg(SrmMessageStatus.fail,typeName,orderData,startDate,endDataStr);
    }
}
